<?php

namespace App\Traits;

use App\Services\Api\Api;

trait CompanyTrait
{
    public function getCompanyInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            if ($result['type'] == 1) {
                $returnInfo = $this->_getServicerInfo($result);
            } else if ($result['type'] == 2) {
                $returnInfo = $this->_getOwnerInfo($result);
            } else {
                $returnInfo = $result;
            }
            return $returnInfo;
        }
        return false;
    }

    public function authCompanyNameInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custName' => $data['companyName'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? $result['custId'] : false;
    }

    private function _getServicerInfo($dataInfo)
    {
        $return = [
            'type' => $dataInfo['type'],
            'auth_status' => $dataInfo['authStatus'],
            'company_name' => $dataInfo['name'],
            'logo_info' => $dataInfo['logo'],
            'company_type_id' => $dataInfo['nature'],
            'legan_user_name' => $dataInfo['legalPerson'],
            'legan_user_phone' => $dataInfo['legalPhone'],
            'legan_user_email' => $dataInfo['legalEmail'],
            'company_regist_date' => $dataInfo['registerTime'],
            'bank_level_id' => $dataInfo['creditRank'],
            'company_regist_money' => $dataInfo['registerCapital']['v'],
            'company_regist_money_type' => $dataInfo['registerCapital']['c'],
            'all_money' => $dataInfo['totalCapital']['v'],
            'earn_money' => $dataInfo['netCapital']['v'],
            'company_regist_address_id' => $dataInfo['registerAddr']['p'] . ',' . $dataInfo['registerAddr']['c'] . ',' . $dataInfo['registerAddr']['a'],
            'company_regist_address' => $dataInfo['registerAddr']['d'],
            'contact_user_name' => $dataInfo['contact'],
            'contact_user_phone' => $dataInfo['telephone'],
            'zip_code' => $dataInfo['postCode'],
            'fix_phone' => $dataInfo['fixphone'],
            'fix_number' => $dataInfo['fax'],
            'contact_user_email' => $dataInfo['email'],
            'contact_user_address_id' => $dataInfo['contactAddr']['p'] . ',' . $dataInfo['contactAddr']['c'] . ',' . $dataInfo['contactAddr']['a'],
            'contact_user_address' => $dataInfo['contactAddr']['d'],
            'company_domain_id' => trim($dataInfo['domain'], ','),
            'company_service_type_id' => $dataInfo['serviceMode'],
            'stuff_num' => $dataInfo['workerNum'],
            'tech_num' => $dataInfo['techNum'],
            'major_business' => $dataInfo['business'],
            'service_domain_id' => trim($dataInfo['serviceDomain'], ','),
            'method_id' => trim($dataInfo['method'], ','),
            'organize_code' => $dataInfo['organizationCode'],
            'organize_code_file' => $dataInfo['code'],
            'organize_regist_number' => $dataInfo['businessLicene'],
            'organize_regist_number_file' => $dataInfo['licene'],
            'company_banner' => $dataInfo['banner'],
            'company_desc' => $dataInfo['introduction'],
            'main_tech_desc' => $dataInfo['techExpertise'],
            'tech_success_desc' => $dataInfo['advContent'],
            'record_info' => $dataInfo['qual'],
            'fgw_file' => $dataInfo['fgwAttach'],
            'gxb_file' => $dataInfo['gxbAttach'],
            'province_file' => $dataInfo['provinceAttach'],
            'city_file' => $dataInfo['cityAttach'],
            'tech_route_info' => @$dataInfo['serviceAbility'],
        ];

        return $return;
    }

    private function _getOwnerInfo($dataInfo)
    {
        $return = [
            'type' => $dataInfo['type'],
            'auth_status' => $dataInfo['authStatus'],
            'company_name' => $dataInfo['name'],
            'logo_info' => $dataInfo['logo'],
            'company_type_id' => $dataInfo['nature'],
            'legan_user_name' => $dataInfo['legalPerson'],
            'legan_user_phone' => $dataInfo['legalPhone'],
            'legan_user_email' => $dataInfo['legalEmail'],
            'company_regist_date' => $dataInfo['registerTime'],
            'company_regist_money' => $dataInfo['registerCapital']['v'],
            'company_regist_money_type' => $dataInfo['registerCapital']['c'],
            'company_regist_address_id' => $dataInfo['registerAddr']['p'] . ',' . $dataInfo['registerAddr']['c'] . ',' . $dataInfo['registerAddr']['a'],
            'company_regist_address' => $dataInfo['registerAddr']['d'],
            'contact_user_name' => $dataInfo['contact'],
            'contact_user_phone' => $dataInfo['telephone'],
            'zip_code' => $dataInfo['postCode'],
            'fix_phone' => $dataInfo['fixphone'],
            'fix_number' => $dataInfo['fax'],
            'contact_user_email' => $dataInfo['email'],
            'contact_user_address_id' => $dataInfo['contactAddr']['p'] . ',' . $dataInfo['contactAddr']['c'] . ',' . $dataInfo['contactAddr']['a'],
            'contact_user_address' => $dataInfo['contactAddr']['d'],
            'company_industry' => $dataInfo['belongField'],
            'company_desc' => $dataInfo['introduction'],
            'organize_code' => $dataInfo['organizationCode'],
            'organize_code_file' => $dataInfo['code'],
            'organize_regist_number' => $dataInfo['businessLicene'],
            'organize_regist_number_file' => $dataInfo['licene'],
        ];
        return $return;
    }

    public function authCompanyOwnerInfo($apiCode, $data)
    {
        $registMoneyAndType = [
            'v' => $data['company_regist_money'],
            'c' => $data['company_regist_money_type'],
        ];
        list($registProvince, $registCity, $registArea) = explode(',', $data['company_regist_address_id']);
        list($contactProvince, $contactCity, $contactArea) = explode(',', $data['contact_user_address_id']);
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
            'phone' => $data['account_phone'],
            'name' => $data['company_name'],
            'type' => 2,
            'logo' => $data['logo_info'],
            'nature' => $data['company_type_id'],
            'legalPerson' => $data['legan_user_name'],
            'legalPhone' => $data['legan_user_phone'],
            'legalEmail' => $data['legan_user_email'],
            'registerTime' => $data['company_regist_date'],
            'registerCapital' => $registMoneyAndType,
            'registerAddr' => [
                'p' => $registProvince,
                'c' => $registCity,
                'a' => $registArea,
                'd' => $data['company_regist_address'],
            ],
            'postCode' => $data['zip_code'],
            // 'creditRank' => -1,
            // 'totalCapital' => '{}',
            // 'netCapital' => '{}',

            'contact' => $data['contact_user_name'],
            'telephone' => $data['contact_user_phone'],
            'fixphone' => $data['fix_phone'],
            'fax' => $data['fix_number'],
            'email' => $data['contact_user_email'],
            'contactAddr' => [
                'p' => $contactProvince,
                'c' => $contactCity,
                'a' => $contactArea,
                'd' => $data['contact_user_address'],
            ],
            // 'domain' => '',
            // 'serviceMode' => -1,
            // 'wokerNum' => -1,
            // 'techNum' => -1,
            // 'business' => '',
            // 'serviceDomain' => '',
            // 'method' => '',

            'organizationCode' => $data['organize_code'],
            'code' => $data['organize_code_file'],
            'businessLicene' => $data['organize_regist_number'],
            'licene' => $data['organize_regist_number_file'],
            // 'banner' => '{}',
            // 'qual' => '{}',
            // 'gxbAttach' => '{}',
            // 'fgwAttach' => '{}',
            // 'provinceAttach' => '{}',
            // 'cityAttach' => '{}',
            'belongField' => $data['company_industry'],
            'introduction' => $data['company_desc'],
            // 'techExpertise' => '',
            // 'advTitle' => '',
            // 'advContent' => '',

        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function authCompanyServicerInfo($apiCode, $data)
    {
        $newTechPath = [];
        if ($data['tech_route']) {
            foreach ($data['tech_route'] as $key => $value) {
                $newTechPath[$key]['id'] = $value['id'];
                $str = '';
                $arr = array_reverse($value['key']);
                foreach ($arr as $key2 => $value2) {
                    $str = $value2 == 'true' ? ($str . '1') : $str . '0';
                }
                $newTechPath[$key]['ab'] = bindec($str);
            }
        }

        $obj = new Api();
        $registMoneyAndType = [
            'v' => $data['company_regist_money'],
            'c' => $data['company_regist_money_type'],
        ];
        $recordFlagList = [
            'gxb' => $data['gxb_record'] ? 1 : 0,
            'fgw' => $data['fgw_record'] ? 1 : 0,
            'province' => $data['province_record_id'] ? $data['province_record_id'] : 0,
            'city' => $data['city_record_id'] ? $data['city_record_id'] : 0,
        ];
        list($registProvince, $registCity, $registArea) = explode(',', $data['company_regist_address_id']);
        list($contactProvince, $contactCity, $contactArea) = explode(',', $data['contact_user_address_id']);
        $formData = [
            'custId' => $data['company_id'],

            'phone' => $data['account_phone'],
            'name' => $data['company_name'],
            'type' => 1,
            'nature' => $data['company_type_id'],

            'logo' => $data['logo_info'],
            'legalPerson' => $data['legan_user_name'],
            'legalPhone' => $data['legan_user_phone'],
            'legalEmail' => $data['legan_user_email'],
            'registerTime' => $data['company_regist_date'],
            'registerCapital' => $registMoneyAndType,
            'registerAddr' => [
                'p' => $registProvince,
                'c' => $registCity,
                'a' => $registArea,
                'd' => $data['company_regist_address'],
            ],
            'postCode' => $data['zip_code'],

            'creditRank' => $data['bank_level_id'],

            'totalCapital' => [
                'v' => $data['all_money'],
                'c' => 1,
            ],
            'netCapital' => [
                'v' => $data['earn_money'],
                'c' => 1,
            ],

            'contact' => $data['contact_user_name'],
            'telephone' => $data['contact_user_phone'],
            'fixphone' => $data['fix_phone'],
            'fax' => $data['fix_number'],
            'email' => $data['contact_user_email'],
            'contactAddr' => [
                'p' => $contactProvince,
                'c' => $contactCity,
                'a' => $contactArea,
                'd' => $data['contact_user_address'],
            ],

            'domain' => $data['company_domain_id'] ? ',' . $data['company_domain_id'] . ',' : '',
            //'serviceMode' => $data['company_service_type_id'],
            'serviceAbility' => $newTechPath, //技术路径及服务能力

            'workerNum' => $data['stuff_num'],
            'techNum' => $data['tech_num'],
            'business' => $data['major_business'],
            'serviceDomain' => $data['service_domain_id'] ? ',' . $data['service_domain_id'] . ',' : '',
            'method' => $data['method_id'] ? ',' . $data['method_id'] . ',' : '',
            'organizationCode' => $data['organize_code'],
            'code' => $data['organize_code_file'],
            'businessLicene' => $data['organize_regist_number'],
            'licene' => $data['organize_regist_number_file'],
            'banner' => $data['company_banner'],
            'qual' => $recordFlagList,
            'gxbAttach' => $data['gxb_record'],
            'fgwAttach' => $data['fgw_record'],
            'provinceAttach' => $data['province_record'],
            'cityAttach' => $data['city_record'],
            'introduction' => $data['company_desc'],
            'techExpertise' => $data['main_tech_desc'],
            'advContent' => $data['tech_success_desc'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : false;
    }

    public function authCompanyServicerInfoFromFront($apiCode, $data)
    {
        $obj = new Api();
        $registMoneyAndType = [
            'v' => 0,
            'c' => 1,
        ];
        $recordFlagList = [
            'gxb' => 0,
            'fgw' => 0,
            'province' => 0,
            'city' => 0,
        ];
        list($registProvince, $registCity, $registArea) = explode(',', $data['company_regist_address_id']);
        $formData = [
            'custId' => $data['company_id'],
            'phone' => $data['account_phone'],
            'name' => $data['company_name'],
            'legalPerson' => $data['legan_user_name'],
            'type' => 1,
            'registerAddr' => [
                'p' => $registProvince,
                'c' => $registCity,
                'a' => $registArea,
                'd' => $data['company_regist_address'],
            ],
            'domain' => $data['company_domain_id'] ? ',' . $data['company_domain_id'] . ',' : '',
            'licene' => $data['organize_regist_number_file'],
            'organizationCode' => $data['organize_code'],
            'code' => $data['organize_code_file'],
            'from_front' => true,
            'serviceAbility' => $data['tech_route'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : ['succ' => $result['succ']];
    }

}
